package Game
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import mx.core.UIComponent;
	
	import spark.components.Group;
	import spark.filters.DropShadowFilter;
	import spark.primitives.BitmapImage;

	public class Bullet extends Group
	{
		
		[Embed(source="../assets/models/missile.png")]
		private var _SheetClassData:Class;
		private var _SheetBitmapData:BitmapData;
		
		private var _detailBitmapImage:BitmapImage;
		private var _maskBitmapImage:Group;
		
		private var _filter:DropShadowFilter = new DropShadowFilter();
		
		private var _position:Point;
		private var _direction:Point;
		
		private var _speed:int=6;
		
		private var _isBouncing:Boolean = false;
		
		private var _collisionCount:int = 0;
		
		public function Bullet(position:Point, direction:Point)
		{
			_position = position;
			_direction = direction;
			
			_SheetBitmapData = new _SheetClassData().bitmapData;
			
			_detailBitmapImage = new BitmapImage();
			_detailBitmapImage.source = new Bitmap(_SheetBitmapData);
			
			_detailBitmapImage.x = -24;
			_detailBitmapImage.y = -8;
			
			_maskBitmapImage = new Group();
			
			_maskBitmapImage.graphics.beginFill(0xFF0000);
			_maskBitmapImage.graphics.drawCircle(8, 8, 8);
			_maskBitmapImage.graphics.endFill();

			_maskBitmapImage.visible = true;

			_maskBitmapImage.x = -8;
			_maskBitmapImage.y = -8;
			
			this.addElement(_maskBitmapImage);
			this.addElement(_detailBitmapImage);
			
			// apply shadow filter
			_filter.blurX = 4;
			_filter.blurY = 4;
			_filter.quality = 10;
			_filter.alpha = 50;
			_filter.angle = 45;
			_filter.color = 0x000000;
			_filter.distance = 8;
			_filter.inner = false;
			
			this.filters = [ _filter ];
			
			this.x = _position.x;
			this.y = _position.y;
			
			// Triangle adjacent angle side distance for the x value.
			var dx:Number = _direction.x - this.x;
			// Triangle opposite angle side distance for the y value.
			var dy:Number = _direction.y - this.y;
			// Compute angle in radians from the sprite to the mouse position.
			var radians:Number = Math.atan2(dy, dx);
			// Convert radians to degrees
			this.rotationZ = (radians * 180 / Math.PI );

			_direction = new Point(
						Math.cos(radians) * _speed,
						Math.sin(radians) * _speed
						);
			
			this.x += _direction.x*6;
			this.y += _direction.y*6;

			//this.scaleX=0.5;
			//this.scaleY=0.5;
		}

		public function get base():Group
		{
			return _maskBitmapImage;
		}

		public function set base(value:Group):void
		{
			_maskBitmapImage = value;
		}

		public function get isBouncing():Boolean
		{
			return _isBouncing;
		}

		public function set isBouncing(value:Boolean):void
		{
			_isBouncing = value;
		}

		public function get speed():int
		{
			return _speed;
		}

		public function set speed(value:int):void
		{
			_speed = value;
		}

		public function get direction():Point
		{
			return _direction;
		}

		public function set direction(value:Point):void
		{
			_direction = value;
		}

		public function get collisionCount():int
		{
			return _collisionCount;
		}

		public function set collisionCount(value:int):void
		{
			_collisionCount = value;
		}

		public function step():void
		{
			this.x += _direction.x;
			this.y += _direction.y;
			
		}
	}
}